/**
 * A singular factory that contains all of the DateTime-factories needed in the SDV processing.
 */
package org.hackystat.ui.hackerrific.client;

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * A singular factory that contains all of the DateTime-factories needed in the SDV processing.
 * @author David J Nickles
 * 
 */
public class DateTimeFactories {
  
  private static DateTimeFactories singular;

  private DateTimeFormat twitterfactory;
  private DateTimeFormat httpfactory;
  private DateTimeFormat gmtfactory;
  private DateTimeFormat xmlfactory;
  private DateTimeFormat timefactory;
  private DateTimeFormat dayfactory;

  /**
   * Construct all the DataTime Factories.
   */
  private DateTimeFactories() {
    initDateTimeFactories();
  }
  
  /**
   * This method constructs the DateTimeFactories that will be used to manipulate timestamps.
   */
  private void initDateTimeFactories() {
    // Twitter time: <created_at>Wed Oct 17 18:53:11 +0000 2007</created_at>
    this.twitterfactory = DateTimeFormat.getFormat("EEE MMM dd HH:mm:ss Z yyyy");
    this.httpfactory = DateTimeFormat.getFormat("EEE,+d+MMM+yyyy+HH:mm:ss+'GMT'");
    this.xmlfactory = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    this.gmtfactory = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+0000'");
    this.timefactory = DateTimeFormat.getFormat("h:mm:ssa");
    this.dayfactory = DateTimeFormat.getFormat("EEE, MMM dd");
  }
  
  /**
   * @return a singular reference to a SDVDateTimeFactory that this class maintains.
   */
  public static DateTimeFactories getInstance() {
    if (singular == null) {
      return (singular = new DateTimeFactories());
    } 
    else {
      return singular;
    }
  }

  /**
   * @return the twitterfactory
   */
  public DateTimeFormat getTwitterfactory() {
    return this.twitterfactory;
  }

  /**
   * @return the twitterfactory
   */
  public DateTimeFormat getHttpfactory() {
    return this.httpfactory;
  }

  /**
   * @return the gmtfactory
   */
  public DateTimeFormat getGMTfactory() {
    return this.gmtfactory;
  }

  /**
   * @return the zonefactory
   */
  public DateTimeFormat getXmlfactory() {
    return this.xmlfactory;
  }

  /**
   * @return the timefactory
   */
  public DateTimeFormat getTimefactory() {
    return this.timefactory;
  }

  /**
   * @return the dayfactory
   */
  public DateTimeFormat getDayfactory() {
    return this.dayfactory;
  }
  
  /**
   * This method will format the time into the appropriate XML Schema time format
   * yyyy-MM-dd'T'hh:mm:ss.SSS ex. 2007-04-30T09:00:00-10:00.
   * @param date the request event date and time
   * @return String Formatted string of the time in proper XML Schema time format.
   */
  public String correctZone(Date date) {
    String gwtZoneDate = getInstance().getXmlfactory().format(date);
    /* GWT bug does not put the : in the time zone value which is needed 
     * on the Java side by XMLGregorianCalendar lexical parser. */
    String javaZoneDate = new String(gwtZoneDate.substring(0, gwtZoneDate.length() - 2) + ":"
        + gwtZoneDate.substring(gwtZoneDate.length() - 2, gwtZoneDate.length()));
    return javaZoneDate;
  }
}
